package code.example.base.reflect.parameter;

import code.example.tool.LogFactory;
import org.springframework.core.DefaultParameterNameDiscoverer;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

public class TestParameter {


    public void showParameter(){
        try {
            Method method = ParameterModel.class.getMethod("deal", new Class[]{String.class, Long.class, Integer.class});
            Parameter[] parameters =  method.getParameters();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(method.getName());
            stringBuilder.append("(");
            DefaultParameterNameDiscoverer defaultParameterNameDiscoverer = new DefaultParameterNameDiscoverer();
            String[] parameterNames = defaultParameterNameDiscoverer.getParameterNames(method);
            for(int i=0;i<parameters.length;i++){
                if(i>0){
                    stringBuilder.append(",");
                }
                Parameter parameter = parameters[i];
                String parameterName = parameter.getName();//这个取不出正确的参数名  因为java8可以通过反射获取参数名，但是编译时需要使用-parameters参数开启这个功能
                String type = parameter.getType().getName();
                stringBuilder.append(type);
                stringBuilder.append(" ");
                stringBuilder.append(parameterNames[i]);
            }
            stringBuilder.append(")");
            LogFactory.getLog().info(stringBuilder.toString());
        }catch (Exception e){
            LogFactory.getLog().error(e);
        }
    }
    public static void main(String args []){
        new TestParameter().showParameter();
    }
}
